package com.winhxd.b2c.common.feign.system;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.constant.ServiceName;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.system.org.condition.SysOrgCondition;
import com.winhxd.b2c.common.domain.system.user.model.SysOrg;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@FeignClient(value = ServiceName.SYSTEM_SERVICE, fallbackFactory = OrgServiceClientFallback.class)
public interface OrgServiceClient {

    /* *
     *查询部门列表
     * @author shumin
     * @date  2019/1/7 0007 14:17
    */
    @RequestMapping(value = "/org/2350/v1/findOrgPageList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseResult<PagedList<SysOrg>> findOrgPageList(@RequestBody SysOrgCondition condition);

   /* *
    *新增部门
    * @author shumin
    * @date  2019/1/7 0007 15:46
   */
    @RequestMapping(value = "/org/2351/v1/saveOrg", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseResult<Void> saveOrg(@RequestBody SysOrg sysOrg);

    /* *
     *修改部门
     * @author shumin
     * @date  2019/1/7 0007 15:56
    */
    @RequestMapping(value = "/org/2352/v1/editOrg", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseResult<Void> editOrg(@RequestBody SysOrg sysOrg);

    /* *
     *根据删除一个部门
     * @author shumin
     * @date  2019/1/7 0007 15:58
    */
    @RequestMapping(value = "/org/2353/v1/deleteOrgById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseResult<Void> deleteOrg(@RequestBody SysOrg sysOrg);

    /* *
     *根据id查询部门
     * @author shumin
     * @date  2019/1/7 0007 20:10
    */
    @RequestMapping(value = "/org/2354/v1/getById/{id}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseResult<SysOrg> getById(@PathVariable("id") Long id);

    /* *
     *查询部门列表
     * @author shumin
     * @date  2019/1/8 0008 10:04
    */
    @RequestMapping(value = "/org/2355/v1/findOrgList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseResult<List<SysOrg>> findOrgList(@RequestBody SysOrgCondition condition);
}

@Component
class OrgServiceClientFallback implements OrgServiceClient, FallbackFactory<OrgServiceClient> {

    private static final Logger logger = LoggerFactory.getLogger(OrgServiceClientFallback.class);

    private Throwable throwable;

    public OrgServiceClientFallback() {
    }

    private OrgServiceClientFallback(Throwable throwable) {
        this.throwable = throwable;
    }

    @Override
    public OrgServiceClient create(Throwable throwable) {
        return new OrgServiceClientFallback(throwable);
    }
    @Override
    public ResponseResult<PagedList<SysOrg>> findOrgPageList(SysOrgCondition condition) {
        logger.error("OrgServiceClientFallback -> findOrgPageList", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<Void> saveOrg(SysOrg sysOrg) {
        logger.error("OrgServiceClientFallback -> saveOrg", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<Void> editOrg(SysOrg sysOrg) {
        logger.error("OrgServiceClientFallback -> editOrg", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<Void> deleteOrg(@RequestBody SysOrg sysOrg) {
        logger.error("OrgServiceClientFallback -> deleteOrgById", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<SysOrg> getById(Long id) {
        logger.error("OrgServiceClientFallback -> getById", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<List<SysOrg>> findOrgList(SysOrgCondition condition) {
        logger.error("OrgServiceClientFallback -> findOrgList", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }
}